package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.WithdrawInfoBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.my.WithdrawPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.StringUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IWithdrawView;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter.BanksAdapter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/6/11.
 * Desc   :
 */

public class WithdrawActivity extends BaseActivity<WithdrawPresenter> implements IWithdrawView {

    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_user_reward_money)
    TextView txtUserRewardMoney;
    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.recyclerview_banks)
    RecyclerView recyclerviewBanks;
    @BindView(R.id.ll_nocard)
    LinearLayout llNocard;

    private String token;
    private BanksAdapter banksAdapter;
    private String card_id;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_withdraw);
    }

    @Override
    protected void initPresenter() {
        mPresenter = new WithdrawPresenter(this);
    }

    @Override
    protected void init() {
        txtTitle.setText("提现");
        token = SharedPreferencesUtil.getsInstances(this).getLoginInfo().getToken();
        mPresenter.getWithDrawInfo(token);
    }

    @OnClick({R.id.ll_leftview, R.id.txt_band_bankcard, R.id.txt_withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.txt_band_bankcard:
                Intent intent = new Intent(this, AddBankCardActivity.class);
                startActivity(intent);
                break;
            case R.id.txt_withdraw:
                if (StringUtils.isEmpty(etInput.getText().toString())){
                    mPresenter.withdraw(token, etInput.getText().toString(), card_id);
                }else {
                    showTip("请输入要提现的金额");
                }
                break;
        }
    }

    @Override
    public void getWithdrawInfo(WithdrawInfoBean withdrawInfoBean) {
        txtUserRewardMoney.setText("￥" + withdrawInfoBean.getMoney());
        if (null != withdrawInfoBean.getCard() && withdrawInfoBean.getCard().size() > 0){
            banksAdapter = new BanksAdapter(mContext, R.layout.item_bankinfo, withdrawInfoBean
                    .getCard());
            recyclerviewBanks.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerviewBanks.setAdapter(banksAdapter);
            card_id = withdrawInfoBean.getCard().get(0).getCa_id();
        }else {
            llNocard.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void withdraw(String result) {
        showTip(result);
        if (result.contains("成功")){
            Intent intent = new Intent(this, WithdrawSuccessActivity.class);
            startActivity(intent);
            finish();
        }
    }

}
